//
//  CYYNetWorkManager+GetMyTakePartyInParties.m
//  LetsParty
//
//  Created by rimi on 15/9/14.
//  Copyright (c) 2015年 LetsPartyTeam. All rights reserved.
//

#import "CYYNetWorkManager+GetMyTakePartyInParties.h"

@implementation CYYNetWorkManager (GetMyTakePartyInParties)
//获得我所有参加的活动
+ (void)getMyTakePartyInParties:(SucessGet)sucessGet
                           fail:(Fail)fail{
    if ([[UserModel defaultUser] isAlreadyLoad]) {
        NSString * objectId = [CYYNetWorkManager currentUserGet].objectId;
        BmobQuery * bquery = [BmobQuery queryWithClassName:@"Parties"];
        //构造约束条件
        BmobQuery *inQuery = [BmobQuery queryWithClassName:@"_User"];
        [inQuery whereKey:@"objectId" equalTo:objectId];
        //匹配查询
        [bquery whereKey:@"partyParticipators" matchesQuery:inQuery];
        //约束2
        //获得当前系统的时间
        NSDate * currentDate = [NSDate date];
        //时间转换成NSString
        NSDateFormatter * dateFormate = [[NSDateFormatter alloc]init];
        [dateFormate setDateFormat:@"yyyy/MM/dd"];
        NSString * currentDateString = [dateFormate stringFromDate:currentDate];
        [bquery whereKey:@"partyEndTime" greaterThanOrEqualTo:currentDateString];

        //降序
        [bquery orderByAscending:@"partyBeginTime"];
        
        [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            if (error) {
                fail(error);
            }else{
                NSMutableArray * dataArray = [array mutableCopy];
                //移除参加者
                [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    if ([[obj objectForKey:@"partySponsorInfo"][@"partySponsorObjectId"] isEqualToString:[CYYNetWorkManager currentUserGet].objectId]) {
                        [dataArray removeObject:obj];
                    }
                }];
                sucessGet(dataArray);
            }
        }];
    }else{
            //用户没有登陆
//            NSLog(@"当前用户没有登陆");
    }
}

@end
